// console.log(process.env.TARGET, "rollup");
const path = require("path");
const json = require("@rollup/plugin-json");
const ts = require("rollup-plugin-typescript2");
// 找到packages 的路径
const packagesDir = path.resolve(__dirname, "packages");
// 找到packages 下的所有文件夹
const packageDir = path.resolve(packagesDir, process.env.TARGET);

// 创建一个通用的解析文件路径的方法
const resolve = (p) => path.resolve(packageDir, p);
const pkg = require(resolve("package.json"));
const name = path.basename(packageDir);
// console.log(pkg, 'json');
const outputConfig = {
  "esm-bundler": {
    file: resolve(`dist/${name}.esm-bundler.js`),
    format: "es",
  },
  "cjs": {
    file: resolve(`dist/${name}.cjs.js`),
    format: "cjs",
  },
  "global": {
    file: resolve(`dist/${name}.global.js`),
    format: "iife",
  },
};
const options = pkg.buildOptions;
export default options.formats.map((format) => {
  // console.log(format);
  return createConfig(format, outputConfig[format]);
});

function createConfig(format, output) {
  output.name = options.name;
  output.sourcemap = true;
  return {
    input: resolve(`src/index.ts`),
    output,
    plugins:[
        json(),
        ts({
            tsconfig: path.resolve(__dirname, "tsconfig.json")
        })
    ]
  };
}
